/**
 * 
 */
package com.study.runtime;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

/**
 * @author Administrator
 * @date 2019年7月14日
 */
public class InvokeByRuntime {
    /**
     * 在Java中调用Python: 直接通过Runtime进行调用
     * @param args
     * @throws IOException 
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws IOException,InterruptedException {
        String exe = "python";
        String command = "D:\\06java\\workspace\\study-demo\\src\\main\\java\\com\\study\\runtime\\calculator.py";
        String num1 = "1";
        String num2 = "2";
        String[] cmdArr = new String[] {exe,command, num1,num2};
        Process process = Runtime.getRuntime().exec(cmdArr);
        InputStream is = process.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readLine();
        process.waitFor();
        System.out.println(str);
        
        System.out.println("invokeByJython>>>>>>>>");
        invokeByJython();
    }
    
    public static void invokeByJython(){
    	
    	System.setProperty("python.home", "D:\\jython2.7.0");
    	
    	PythonInterpreter pythonInterpreter = new PythonInterpreter();
    	// 加载python程序
    	pythonInterpreter.execfile("D:\\06java\\workspace\\study-demo\\src\\main\\java\\com\\study\\runtime\\calculator.py");
    	// 调用Python程序中的函数
    	PyFunction pyFunction = pythonInterpreter.get("add",PyFunction.class);
    	PyObject __call__ = pyFunction.__call__(Py.newInteger(1), Py.newInteger(1));
    	System.out.println(__call__);
    	
    }
}
